package com.yunxun.utils;

import com.yunxun.entity.MessageEntity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DownWebPageUtil {
    /**
     * 获得指定网页的html代码
     * @param url
     * @param encoding
     * @return
     */
    public  String getHtmlCode(String url,String encoding){
        URL uri;
        URLConnection urlConnection;
        InputStream inputStream;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader =null;
        StringBuffer stringBuffer = null;
        try {
            uri=new URL(url);
            urlConnection = uri.openConnection();
            inputStream = urlConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream,encoding);
            bufferedReader = new BufferedReader(inputStreamReader);
            String line = null;
            stringBuffer =new StringBuffer();
            while ((line = bufferedReader.readLine()) != null){
                stringBuffer.append(line + "\n");
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bufferedReader != null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 确定采集内容的区域块
     * @param url
     * @param encoding
     * @return
     */
     private  String getTargetArea(String url,String encoding){
        String htmlCode = getHtmlCode(url,encoding);
        String reg = "<div class=\"items-area\">[\\s\\S]*</div>";
        Pattern pattern= Pattern.compile(reg);
        Matcher matcher = pattern.matcher(htmlCode);
        String area = null;
        if(matcher.find()){
            area = matcher.group();
        }
        return area;
    }

    /**
     * 获得要采集内容的目标块
     * @param url
     * @param encoding
     * @return
     */
     private  List<String> getTargetBlocks(String url, String encoding){
        String area = getTargetArea(url,encoding);
        List<String> targetBlocks = new ArrayList<String>();
        String con = "<div class=\"item\">[\\s\\S]*?</div>";
        Pattern pattern = Pattern.compile(con);
        Matcher matcher = pattern.matcher(area);
        while (matcher.find()){
            if(!targetBlocks.contains(matcher.group())){
                targetBlocks.add(matcher.group());
            }
        }
        return targetBlocks;
    }


     public  List<MessageEntity> getMessageEntities(String url,String encoding,int typeValue){
        List<MessageEntity> messageEntities=new ArrayList<MessageEntity>();
        List<String> targetBlocks = getTargetBlocks(url,encoding);
        Pattern pattern;
        Matcher matcher;
        //图片链接
        String imageUrl;
        //标题链接和标题名称
         String titleKey;
         String titleUrl;
        //简介
        String intro ;
        //类型名称和链接url
         String typeKey;
         String typeUrl;
        //发布时间
        String issureTime;
        //相关链接推荐
//        Map<String,String> relatedLinks ;
//       广告标题
//        String advertisingTitle;
        String reg;
        String s ;
        int beginIndex;
        int endIndex;

        for(String targetBlock:targetBlocks){
            //找倒图片链接
            reg = "<img(.*)(src=\".*?\")(.*?)>";
            pattern = Pattern.compile(reg);
            matcher = pattern.matcher(targetBlock);
            MessageEntity messageEntity = new MessageEntity();
            //因为目标块里只有一张图片
            if(matcher.find()){
                s = matcher.group();
                beginIndex = s.indexOf("src=\"")+5;
                endIndex = s.indexOf("\"",beginIndex+1);
                imageUrl = s.substring(beginIndex,endIndex);
                messageEntity.setImageUrl(imageUrl);
            }
            //找到标题链接和标题名称
            reg = "<dt>[\\s\\S]*?</dt>";
            pattern = Pattern.compile(reg);
            matcher = pattern.matcher(targetBlock);
            if(matcher.find()){
                s = matcher.group();
                beginIndex = s.indexOf("href=\"")+6;
                endIndex = s.indexOf("\"",beginIndex+1);
                titleUrl = s.substring(beginIndex,endIndex);
                if(!titleUrl.contains("https:")){
                    titleUrl ="https:"+titleUrl;
                }
                beginIndex = s.indexOf(">",endIndex)+1;
                endIndex = s.indexOf("<",endIndex);
                titleKey = s.substring(beginIndex,endIndex);
                messageEntity.setTitle(titleKey);
                messageEntity.setTitleUrl(titleUrl);
            }
            //找到简介
             reg = "<p>[\\s\\S]*?</p>";
             pattern = Pattern.compile(reg);
             matcher = pattern.matcher(targetBlock);
             if(matcher.find()){
                 s = matcher.group();
                 beginIndex = s.indexOf("<p>")+3;
                 endIndex = s.indexOf("</p>");
                 intro = s.substring(beginIndex,endIndex);
                 messageEntity.setIntro(intro);
             }
             //获得类型
             reg = "<label.*>[\\s\\S]*?</label>";
             pattern = Pattern.compile(reg);
             matcher = pattern.matcher(targetBlock);
             if(matcher.find()){
                 s = matcher.group();
                 if(s.contains("href")){
                     beginIndex = s.indexOf("href=\"")+6;
                     endIndex = s.indexOf("\"",beginIndex+1);
                     typeUrl = s.substring(beginIndex,endIndex);
                     if(!typeUrl.contains("https:")){
                         typeUrl = "https:"+typeUrl;
                     }
                     beginIndex = s.indexOf(">",endIndex)+1;
                     endIndex = s.indexOf("<",endIndex);
                     typeKey = s.substring(beginIndex,endIndex);
                     messageEntity.setType(typeKey);
                     messageEntity.setTypeUrl(typeUrl);
                 }else {
                     beginIndex = s.indexOf(">")+1;
                     endIndex = s.lastIndexOf("<");
                     typeKey = s.substring(beginIndex,endIndex);
                     messageEntity.setType(typeKey);
                 }

             }
             //获得发布时间阅读次数和评论意见个数
             reg = "<li>发布于.*</li>";
             pattern = Pattern.compile(reg);
             matcher =pattern.matcher(targetBlock);
             if(matcher.find()){
                 s = matcher.group();
                 String[] strs = s.split("&nbsp;&nbsp;");
                 for(String str:strs){
                     if(str.contains("发布于")){
                         beginIndex = str.indexOf("发布于")+3;
                         messageEntity.setIssureTime(str.substring(beginIndex));
                     }else if(str.contains("次阅读")){
                         beginIndex = 0;
                         endIndex = str.indexOf("次阅读");
                         messageEntity.setReadingTimes(str.substring(beginIndex,endIndex));
                     }else if(str.contains("个意见")){
                         beginIndex = 0;
                         endIndex = str.indexOf("个意见");
                         messageEntity.setCommentCount(str.substring(beginIndex,endIndex));
                     }else{
                        continue;
                     }
                 }
             }
             //相关链接推荐
//            relatedLinks =new HashMap<String, String>();
//             reg = "<div class=\"cnbeta-update-list-article\">[\\s\\S]*?</div>";
//             pattern =Pattern.compile(reg);
//             matcher = pattern.matcher(targetBlock);
//             if(matcher.find()){
//                 s = matcher.group();
//                 reg ="<li>[\\s\\S]*?</li>";
//                 pattern =Pattern.compile(reg);
//                 matcher =pattern.matcher(s);
                 //<li>标签里的内容
//                 String liCon = "";
//                 while(matcher.find()){
//                     liCon = matcher.group();
//                     beginIndex = s.indexOf("href=\"")+6;
//                     endIndex = s.indexOf("\"",beginIndex+1);
//                     String relatedLinkKey = s.substring(beginIndex,endIndex);
//                     if(!relatedLinkKey.contains("https:")){
//                         relatedLinkKey +="https:"+relatedLinkKey;
//                     }
//                     beginIndex = s.indexOf(">")+1;
//                     endIndex = s.lastIndexOf("<");
//                     String relatedLinkValue = s.substring(beginIndex,endIndex);
//                     relatedLinks.put(relatedLinkKey,relatedLinkValue);
//                 }
//                 messageEntity.setRelatedLinks(relatedLinks);
//             }
//             reg = "<b>.*?</b>";
//             pattern =Pattern.compile(reg);
//             matcher = pattern.matcher(targetBlock);
//             if(matcher.find()){
//                 s = matcher.group();
//                 beginIndex = s.indexOf(">")+1;
//                 endIndex = s.lastIndexOf("<");
//                 advertisingTitle = s.substring(beginIndex,endIndex);
//                 messageEntity.setAdvertisingTitle(advertisingTitle);
//             }
             messageEntities.add(messageEntity);
        }
        return messageEntities;
    }
}
